package com.cb.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cb.staservice.client.UcenterClient;
import com.cb.staservice.entity.StatisticsDaily;
import com.cb.staservice.mapper.StatisticsDailyMapper;
import com.cb.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cb.staservice.utils.DateUtil;
import com.cb.utils.R;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Cebb
 * @since 2020-07-01
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    private final UcenterClient ucenterClient;

    @Autowired
    public StatisticsDailyServiceImpl(UcenterClient ucenterClient) {
        this.ucenterClient = ucenterClient;
    }

    @Override
    public void registerCount(String day) {
        // 删除相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        // 远程调用 , 查询注册人数
        R registerResult = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerResult.getData().get("countRegister");

        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(countRegister);
        daily.setDateCalculated(day);
        daily.setVideoViewNum(RandomUtils.nextInt(100,200));
        daily.setCourseNum(RandomUtils.nextInt(100,200));
        daily.setLoginNum(RandomUtils.nextInt(100, 200));
        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        Map<String, Object> result = new HashMap<>(5);
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.select("date_calculated",type);
        if (StringUtils.isEmpty(begin) && StringUtils.isEmpty(begin)) {
            Date date = new Date();
            wrapper.between("date_calculated",
                    DateUtil.formatDate(DateUtil.addDays(date, -30)),
                    DateUtil.formatDate(date));
        }else {
            if (!StringUtils.isEmpty(begin)) {
                wrapper.ge("date_calculated", begin);
            }
            if (!StringUtils.isEmpty(begin)) {
                wrapper.le("date_calculated", end);
            }
        }
        wrapper.between("date_calculated", begin, end);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        ArrayList<String> dateCalculatedList = new ArrayList<>();
        ArrayList<Integer> dataList = new ArrayList<>();
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            dateCalculatedList.add(daily.getDateCalculated());
            switch(type){
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        result.put("date_calculatedList", dateCalculatedList);
        result.put("numDataList", dataList);
        return result;
    }

}
